--
-- Author: mm
-- Date: 2018-05-07 18:27:09
--
-- GameplaypreviewMidRender
--

local string_split = string.split

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.GamePlayPreviewNode_2))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )
    self:refresh()
end

-- 自动适配文本高度
local function _autoJustTextHeight( noteNode )
    local size = noteNode:getContentSize()
    noteNode:getVirtualRenderer():setDimensions( size.width, 0 )
    local afterSize = noteNode:getVirtualRendererSize()
    noteNode:setContentSize( cc.size( size.width, afterSize.height ) )
    ccui.Helper:doLayout( noteNode )
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )
        local data = self._data

        local bgPath = ""
        if string.find( data.funcBg, ";" ) then
            local bgList = string_split( data.funcBg, ";" )
            for i, v in ipairs( bgList ) do
                local tempList = string_split( v, "|" )
                if UD:getClass() == tonumber( tempList[1] ) then -- 自己职业？
                    bgPath = GD:getImagePath( Res.gamePlayPath, tempList[2] )
                    break
                end
            end
        else
            bgPath = GD:getImagePath( Res.gamePlayPath, data.funcBg )
        end
        self._ui.img:loadTexture( bgPath )

        local sloganStr = ""
        if string.find( data.slogan, ";" ) then -- 需要根据职业解析文本
            local sloganList = string_split( data.slogan, ";" )
            for i, v in ipairs( sloganList ) do
                local tempList = string_split( v, "|" )
                if UD:getClass() == tonumber( tempList[1] ) then -- 自己职业？
                    sloganStr = L( tempList[2] )
                    break
                end
            end
        else
            sloganStr = L( data.slogan )
        end
        self._ui.text:setString( sloganStr )
        _autoJustTextHeight( self._ui.text )

        -- 1=技能
        -- 2=装备
        -- 3=地图
        -- 4=功能
        -- 5=副本
        -- 6=专精
        local contentType = data.content
        if 1 ~= contentType and 2 ~= contentType then
            self._ui.img:setVisible( true )
            if 6 == contentType then
                local titleStr = ""
                if string.find( data.title, ";" ) then -- 需要根据职业解析文本
                    local titleList = string_split( data.title, ";" )
                    for i, v in ipairs( titleList ) do
                        local tempList = string_split( v, "|" )
                        if UD:getClass() == tonumber( tempList[1] ) then -- 自己职业？
                            titleStr = L( tempList[2] )
                            break
                        end
                    end
                else
                    titleStr = L( data.title )
                end
                self._ui.tlt:setString( titleStr )
            else
                self._ui.tlt:setString( L( data.title ) )
            end
        end

        local unlockList = string_split( data.unlock, "|" )
        local typeTemp = tonumber( unlockList[1] )
        if 1 == typeTemp then -- 等级？
            if tonumber( unlockList[2] ) > UD:getLevel() then -- 等级不足，未解锁？
                self._ui.tips:setString( L( data.tips ) )
            else
                self._ui.tips:setString( L( data.finish ) )
            end
        elseif 2 == typeTemp then -- 任务？
            if UD:isMainTaskFinished( unlockList[2] ) then -- 任务完成？
                self._ui.tips:setString( L( data.finish ) )
            else
                self._ui.tips:setString( L( data.tips ) )
            end
        end
    else
        self:setVisible( false )
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
